package com.vgemv.jsutilitysdk.Menu.JsMenuFragment;

import android.content.Context;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.widget.ScrollView;

public class JsMenuScrollView extends ScrollView {

    private int clickStartY;
    private int mTouchSlop = 0;

    public JsMenuScrollView(Context context) {
        super(context);
        setOverScrollMode(OVER_SCROLL_ALWAYS);
        setOverScrollMode(OVER_SCROLL_NEVER);
    }


    @Override
    public boolean onInterceptTouchEvent(MotionEvent e) {
//        JsLogManager.logc_d("JsMenuScrollView/onInterceptTouchEvent","");
        int action = e.getAction();
        if(mTouchSlop == 0) {
            return true;
        }
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                //点击按下,记录此时的坐标.
//                JsLogManager.logc_d("JsMenuScrollView/onInterceptTouchEvent","Just click down,Y is %d",clickStartY);
                clickStartY = (int) e.getRawY();
//                if(clickStartY < 100) {
                    return false;
//                }
//                break;
            case MotionEvent.ACTION_MOVE:
                int currentY = (int) e.getRawY();
                int offsetY = currentY - clickStartY;
//                JsLogManager.logc_d("JsMenuScrollView/onInterceptTouchEvent","Click down and move,click start Y is %d,current Y is %d,offset is %d",clickStartY,currentY,offsetY);
                if (Math.abs(offsetY) > mTouchSlop) {
                    return true;
                }
                break;
            case MotionEvent.ACTION_CANCEL:
//                JsLogManager.logc_d("JsMenuScrollView/onInterceptTouchEvent","Action cancel");
                break;
        }
        return super.onInterceptTouchEvent(e);
    }


    @Override
    //返回false根本划不动.
    public boolean onTouchEvent(MotionEvent ev) {
//        JsLogManager.logc_d("JsMenuScrollView/onTouchEvent","");
        super.onTouchEvent(ev);
        return true;
    }

    @Override
    public boolean dispatchKeyEvent (KeyEvent event) {
//        JsLogManager.logc_d("JsMenuScrollView/dispatchKeyEvent","Action cancel");
        return false;
    }

    @Override
    /**
     * 这个方法实现松手之后的继续滚动的效果,速度越来越慢,最后为0.
     */
    public void fling (int velocityY) {
//        JsLogManager.logc_d("JsMenuScrollView/fling","VelocityY is %d",velocityY);
        super.fling(velocityY);
    }

    @Override
    protected boolean overScrollBy(int deltaX, int deltaY, int scrollX, int scrollY, int scrollRangeX, int scrollRangeY, int maxOverScrollX, int maxOverScrollY, boolean isTouchEvent) {
//        JsLogManager.logc_d("JsMenuScrollView/overScrollBy","deltaX:%d,deltaY:%d,scrollX:%d,scrollY:%d,scrollRangeX:%d,scrollRangeY:%d,maxOverScrollX:%d,maxOverScrollY:%d,isTouchEvent:%b",deltaX,deltaY,scrollX,scrollY,scrollRangeX,scrollRangeY,maxOverScrollX,maxOverScrollY,isTouchEvent);
        maxOverScrollY = 200;
        return super.overScrollBy(deltaX, deltaY, scrollX, scrollY, scrollRangeX, scrollRangeY, maxOverScrollX, maxOverScrollY, isTouchEvent);
    }

    @Override
    protected void onOverScrolled(int scrollX, int scrollY, boolean clampedX, boolean clampedY) {
        super.onOverScrolled(scrollX, scrollY, clampedX, clampedY);
    }
}